Page({
  data: {
    // params: {
    //   page: 1,
    //   size: 10,
    //   filterType: 0,
    //   name: '',
    //   isFuzzySearch: true
    // },

    params: {
      pageNo: 1,
      pageSize: 10,
      name: "",
      adultTypeName: ""
    },
    list: [],
    tabIndex: ""
  },


  tabClick(e) {
    // console.log(e.currentTarget.dataset.tab)


    var _this = this
    var tabIndex = e.currentTarget.dataset.tab

    _this.setData({
      params: {
        pageNo: 1,
        pageSize: 10,
        name: "",
        ageLeveName: tabIndex
      },
      tabIndex: tabIndex,
      list: [],
    })



    // this.setState({
    //   ageLeveName2: tabIndex
    // })


    _this.getList(_this.data.params)
  },


  bindname(e) {
    this.setData({
      list: []
    })
    // console.log(e)

    var name = e.detail.value

    var name1 = "params.name"

    this.setData({
      [name1]: name
    })

    this.getList(this.data.params)
  },


  getList(params) {
    var _this = this

    var url = wx.getStorageSync('token') != '' ? 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/findBookInfoByAgeLeve/auth' :'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/findBookInfoByAgeLeve'
    wx.request({
      url: url,
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          _this.setData({
            total: res.data.count
          })
          var list = _this.data.list
          for (var i = 0; i < res.data.data.length; i++) {
            res.data.data[i].addShop = false
            list.push(res.data.data[i])
          }
          _this.setData({
            list: list
          })

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  //书本详情
  bookdetail(e) {
    console.log(e)
    var bookId = e.currentTarget.dataset.bookid
    wx.navigateTo({
      url: '/package/index/pages/bookdetail/bookdetail?bookId=' + bookId,
    })
  },


  //加入购物车

  addShopcar(e) {
    console.log(e.currentTarget.dataset.bookid)

    var bookid = e.currentTarget.dataset.bookid

    for (var i = 0; i < this.data.list.length; i++) {
      if (this.data.list[i].id == bookid) {
        this.addCarApi(bookid, i)
      } else {
        var iscllect = "list[" + i + "]addShop"
        this.setData({
          [iscllect]: false
        })
      }
    }


  },


  //添加购物车

  addCarApi(id, index) {

    var _this = this
    var params = {
      userId: wx.getStorageSync('userId'),
      bookInfoId: id
    }

    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/sdxShoppingTrolleys/mod/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        console.log(res)

        if (res.data.code == 200) {
          var addShop = "list[" + index + "].addShop"

          _this.setData({
            [addShop]: true
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },



  onReachBottom() {
    this.data.params.pageNo += 1
    this.getList(this.data.params)
  },

  onLoad(options) {


    wx.setNavigationBarTitle({
      title: options.titlename
    })


    var adultTypeName = "params.adultTypeName"
    this.setData({


      [adultTypeName]: options.titlename
    })


    this.getList(this.data.params)


    // this.getList(this.data.params)
  }


})
